package com.ruoyi.web.controller.business;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.domain.dto.DtoHis;
import com.ruoyi.system.domain.vo.OutGoodsVo;
import com.ruoyi.system.domain.vo.VoHis;
import com.ruoyi.system.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 出入车辆历史信息Controller
 * 
 * @author ruoyi
 * @date 2024-05-17
 */
@RestController
@RequestMapping("/business/his")
public class TbVehicleHisController extends BaseController
{
    @Autowired
    private ITbVehicleHisService tbVehicleHisService;
    @Autowired
    private ITbVisitorVehicleService tbVisitorVehicleService;
    @Autowired
    private ISysDictDataService dictDataService;
    @Autowired
    private ITbFollowerService tbFollowerService;
    @Autowired
    private ITbGoodsService tbGoodsService;
    @Autowired
    private ITbOutGoodService tbOutGoodService;

    /**
     * 查询出入车辆历史信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(DtoHis tbVehicleHis)
    {
        startPage();
        List<TbVehicleHis> list = tbVehicleHisService.selectTbVehicleHisList(tbVehicleHis);

        if (!list.isEmpty()) {
            List<VoHis> hisList = new ArrayList<VoHis>();
            for (TbVehicleHis itemHis : list) {

                VoHis his = new VoHis();
                BeanUtils.copyProperties(itemHis, his);

                Long pid = itemHis.getPid();

                if (pid != null && pid.intValue() > 0) {
                    TbVisitorVehicle item = tbVisitorVehicleService.selectTbVisitorVehicleById(pid);

                    if (item != null) {

                        String[] ignoreProperties = {"id","card","inTime","outTime"};
                        BeanUtils.copyProperties(item, his, ignoreProperties);


                        if (StringUtils.isNotBlank(item.getCompanyTo())) {
                            his.setCompanyToName(DictUtils.getDictLabel("visiting_unit", item.getCompanyTo()));
                        }

                        if (StringUtils.isNotBlank(item.getVisitingBase())) {
                            his.setVisitingBaseName(DictUtils.getDictLabel("visiting_base", item.getVisitingBase()));
                        }

//                if (StringUtils.isNotBlank(item.getWeighingItems())) {
//                    vo.setWeighingItemsName(dictDataService.selectDictLabel("weighing", item.getWeighingItems()));
//                }

                        if (StringUtils.isNotBlank(item.getCommunicationRoom())) {
                            his.setCommunicationRoomName(dictDataService.selectDictLabel("in_post", item.getCommunicationRoom()));
                        }

                        if (StringUtils.isNotBlank(item.getVehicleType())) {
                            his.setVehicleTypeName(dictDataService.selectDictLabel("vehicle_type", item.getVehicleType()));
                        }

                        String customersType = item.getCustomersType();
                        String visitorType = item.getVisitorType();

                        his.setCustomersTypeName(DictUtils.getDictLabel("visitor_vehicle", customersType));
                        his.setVisitorTypeName(DictUtils.getDictLabel(customersType, visitorType));

                        if (StringUtils.isNotBlank(item.getIfGoods())) {
                            if ("0".equals(item.getIfGoods())) {
                                his.setIfWeighingName("否");
                                his.setGoodsList(new ArrayList<TbGoods>());
                            } else{
                                his.setIfWeighingName("是");
                                TbGoods gItem = new TbGoods();
                                gItem.setPid(item.getId());
                                List<TbGoods> goods = tbGoodsService.selectTbGoodsList(gItem);
                                if (goods != null && goods.size() > 0) {
                                    his.setGoodsList(goods);
                                } else {
                                    his.setGoodsList(new ArrayList<TbGoods>());
                                }
                            }
                        }

                        if (StringUtils.isNotBlank(item.getIfFollower())) {
                            if ("0".equals(item.getIfFollower())) {
                                his.setIfFollowerName("无");
                            } else{
                                his.setIfFollowerName("有");
                            }
                        }

                        if (StringUtils.isNotBlank(item.getDriverLicense())) {
                            his.setDriverLicenseList(item.getDriverLicense().split(","));
                        }
                        if (StringUtils.isNotBlank(item.getDrivingLicense())) {
                            his.setDrivingLicenseList(item.getDrivingLicense().split(","));
                        }

                        TbFollower fItem = new TbFollower();
                        fItem.setPid(item.getId());
                        List<TbFollower> fList = tbFollowerService.selectTbFollowerList(fItem);
                        his.setFollowerList(fList);

                        TbOutGood outGoodSearch = new TbOutGood();
                        outGoodSearch.setPid(item.getId());
                        List<TbOutGood> outGoods = tbOutGoodService.selectTbOutGoodList(outGoodSearch);

                        String inList = "";
                        String outList = "";
                        if (outGoods != null && outGoods.size() > 0) {
                            for (TbOutGood goodItem : outGoods) {

                                if ("0".equals(goodItem.getFlag())) {
                                    inList = inList + goodItem.getImage() + ",";
                                }
                                if ("1".equals(goodItem.getFlag())) {
                                    outList = outList + goodItem.getImage() + ",";
                                }
                            }
                        }

                        if (StringUtils.isNotBlank(inList)) {
                            inList = Arrays.stream(inList.split(",")).filter(s -> !s.isEmpty()).reduce((a, b) -> a + "," + b).orElse("");
                        }

                        if (StringUtils.isNotBlank(outList)) {
                            outList = Arrays.stream(outList.split(",")).filter(s -> !s.isEmpty()).reduce((a, b) -> a + "," + b).orElse("");
                        }

                        OutGoodsVo outGoodsVo = new OutGoodsVo();
                        outGoodsVo.setImgIns(inList);
                        outGoodsVo.setImgOuts(outList);
                        List<OutGoodsVo> outGoodsVos = new ArrayList<OutGoodsVo>();
                        outGoodsVos.add(outGoodsVo);
                        his.setOutGoodsList(outGoodsVos);





                    }









                }

                hisList.add(his);
            }
            TableDataInfo rspData = getDataTable(hisList);
            rspData.setTotal(tbVehicleHisService.selectTbVehicleHisCnt(tbVehicleHis));
            return rspData;
        }
        return getDataTable(list);
    }

    /**
     * 导出出入车辆历史信息列表
     */
    @Log(title = "出入车辆历史信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DtoHis tbVehicleHis)
    {
        List<TbVehicleHis> list = tbVehicleHisService.selectTbVehicleHisExeclList(tbVehicleHis);
        ExcelUtil<TbVehicleHis> util = new ExcelUtil<TbVehicleHis>(TbVehicleHis.class);
        util.exportExcel(response, list, "出入车辆历史信息数据");
    }

    /**
     * 获取出入车辆历史信息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tbVehicleHisService.selectTbVehicleHisById(id));
    }

    /**
     * 新增出入车辆历史信息
     */
    @Log(title = "出入车辆历史信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbVehicleHis tbVehicleHis)
    {
        return toAjax(tbVehicleHisService.insertTbVehicleHis(tbVehicleHis));
    }

    /**
     * 修改出入车辆历史信息
     */
    @Log(title = "出入车辆历史信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbVehicleHis tbVehicleHis)
    {
        return toAjax(tbVehicleHisService.updateTbVehicleHis(tbVehicleHis));
    }

    /**
     * 删除出入车辆历史信息
     */
    @Log(title = "出入车辆历史信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tbVehicleHisService.deleteTbVehicleHisByIds(ids));
    }
}
